<template>
  <div class="app-container">
    <el-form :model="queryParams" ref="queryForm" :inline="true" v-show="showSearch" label-width="68px">
      <el-form-item label="设备型号" prop="devicemodel">
        <el-input
          v-model="queryParams.devicemodel"
          placeholder="设备型号"
          clearable
          size="small"
          @keyup.enter.native="handleQuery"
        />
      </el-form-item>
      <el-form-item>
        <el-button type="primary" icon="el-icon-search" size="mini" @click="handleQuery">搜索</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <el-row :gutter="10" class="mb8">

      <el-col :span="1.5">
        <el-button
          type="success"
          plain
          icon="el-icon-edit"
          size="mini"
          :disabled="single"
          @click="handleUpdate"
          v-hasPermi="['zyx:record:edit']"
        >修改</el-button>
      </el-col>

      <el-col :span="1.5">
        <el-button
          type="warning"
          plain
          icon="el-icon-download"
          size="mini"
          @click="handleExport"
          v-hasPermi="['zyx:record:export']"
        >导出</el-button>
      </el-col>
      <right-toolbar :showSearch.sync="showSearch" @queryTable="getList"></right-toolbar>
    </el-row>

    <el-table v-loading="loading" :data="recordList" @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="单位名称" align="center" prop="zyxDevice.unitname" />
      <el-table-column label="科室/部门" align="center" prop="officename" />
      <el-table-column label="设备类别" align="center" prop="zyxDevice.devicetype" />
      <el-table-column label="设备型号" align="center" prop="zyxDevice.devicemodel" />
      <el-table-column label="生产厂家" align="center" prop="zyxDevice.factory" />
      <el-table-column label="维修工单" align="center">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-info"
            @click="srch(scope.row,'1')"
          >查看</el-button>

        </template>
      </el-table-column>
      <el-table-column label="保养工单" align="center" >
        <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-info"
              @click="srch(scope.row,'2')"
            >查看</el-button>

          </template>
        </el-table-column>
      <el-table-column label="检定工单" align="center" >
        <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-info"
              @click="srch(scope.row,'3')"
            >查看</el-button>

          </template>
        </el-table-column>
      <el-table-column label="运营工单" align="center" >
        <template slot-scope="scope">
            <el-button
              size="mini"
              type="text"
              icon="el-icon-info"
              @click="srch(scope.row,'4')"
            >查看</el-button>

          </template>
        </el-table-column>
      <el-table-column label="备注信息" align="center" prop="remark" />
       <el-table-column label="附件" align="center" prop="annex" >
         <template slot-scope="scope">
           <div  v-for="(item, index) in (scope.row.annex|| '').split(',') " :key="index" v-if="item!==''">
             <el-link type="primary" :href="item" target="_blank">
               附件{{index}}<br>
               </el-link>
           </div>


           </template>
         </el-table-column>
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
        <template slot-scope="scope">
          <el-button
            size="mini"
            type="text"
            icon="el-icon-info"
            @click="handleUpdate(scope.row)"
            v-hasPermi="['zyx:record:edit']"
          >修改</el-button>

        </template>
      </el-table-column>
    </el-table>

    <pagination
      v-show="total>0"
      :total="total"
      :page.sync="queryParams.pageNum"
      :limit.sync="queryParams.pageSize"
      @pagination="getList"
    />

    <!-- 添加或修改设备档案信息对话框 -->
    <el-dialog :title="title" :visible.sync="open" width="500px" append-to-body :close-on-click-modal="false">
      <el-form ref="form" :model="form" :rules="rules" label-width="80px">
        <el-form-item label="医院" prop="remark">
          {{dept.deptName}}
        </el-form-item>
        <el-form-item label="科室" >
           {{form.officename}}
        </el-form-item>
        <el-form-item label="设备编号" >
           {{device.devicecode}}
        </el-form-item>
        <el-form-item label="设备型号" >
          {{device.devicemodel}}
        </el-form-item>

        <el-form-item label="验收材料">
          <fileUpload v-model="form.checkdata" />
          <!-- <file-upload v-model="form.checkdata" ref="checkdataRef" fileSize="10"></file-upload> -->
          <!-- <FileUploads v-model="form.checkdata"></FileUploads> -->
        </el-form-item>
        <el-form-item label="供应商资料">
          <fileUpload v-model="form.supplierdata"/>
        </el-form-item>
        <el-form-item label="备注信息" prop="remark" >
          <el-input v-model="form.remark" type="textarea" placeholder="请输入内容" maxlength="500" />
        </el-form-item>
        <el-form-item label="附件资料">
          <fileUpload v-model="form.annex"/>
        </el-form-item>


      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="submitForm"  v-preventReClick="3000">确 定</el-button>
        <el-button @click="cancel">取 消</el-button>
      </div>
    </el-dialog>
    <record-send-order-list ref="recordSendOrderList1"></record-send-order-list>
  </div>
</template>

<script>
import { listRecord, getRecord, delRecord, addRecord, updateRecord, exportRecord } from "@/api/zyx/record";
import FileUpload from '@/components/FileUpload';
import FileUploads from '@/components/FileUploads';
import recordSendOrderList from './recordSendOrderList.vue';
export default {
  name: "Record",
  components: {
    FileUpload,
    FileUploads,
    recordSendOrderList,
  },
  data() {
    return {
      devicetypeOptions:[],
      // 遮罩层
      loading: true,
      // 选中数组
      ids: [],
      // 非单个禁用
      single: true,
      // 非多个禁用
      multiple: true,
      // 显示搜索条件
      showSearch: true,
      // 总条数
      total: 0,
      // 设备档案信息表格数据
      recordList: [],
      // 弹出层标题
      title: "",
      // 是否显示弹出层
      open: false,
      // 查询参数
      queryParams: {
        pageNum: 1,
        pageSize: 10,
        devicemodel: null,
      },
      // 表单参数
      form: {},
      device: {},
      dept:{},
      // 表单校验
      rules: {
      }
    };
  },
  created() {
    this.getList();
    this.getDicts("zyx_devicetype").then(response => {
      this.devicetypeOptions = response.data;
    });
  },
  methods: {
    //查看工单
    srch(row,type){
      this.$refs.recordSendOrderList1.visible = true;
       this.$refs.recordSendOrderList1.queryParams.deviceid=row.deviceid;
       this.$refs.recordSendOrderList1.queryParams.sendtype=type
        this.$refs.recordSendOrderList1.getList();
    },
    // 设备类别字典字典翻译
    devicetypeFormat(row, column) {
      return this.selectDictLabel(this.devicetypeOptions, row.zyxDevice.devicetype);
    },
    /** 查询设备档案信息列表 */
    getList() {
      this.loading = true;
      listRecord(this.queryParams).then(response => {
        this.recordList = response.rows;
        this.total = response.total;
        this.loading = false;
      });
    },
    // 取消按钮
    cancel() {
      this.open = false;
      this.reset();
    },
    // 表单重置
    reset() {
      this.form = {
        id: null,
        officename:null,
        departid: null,
        deviceid: null,
        checkdata: null,
        supplierdata: null,
        createBy: null,
        createTime: null,
        updateBy: null,
        updateTime: null,
        remark: null,
        delFlag: null,
        annex:null
      };
      this.resetForm("form");
    },
    /** 搜索按钮操作 */
    handleQuery() {
      this.queryParams.pageNum = 1;
      this.getList();
    },
    /** 重置按钮操作 */
    resetQuery() {
      this.resetForm("queryForm");
      this.handleQuery();
    },
    // 多选框选中数据
    handleSelectionChange(selection) {
      this.ids = selection.map(item => item.id)
      this.single = selection.length!==1
      this.multiple = !selection.length
    },
    /** 新增按钮操作 */
    handleAdd() {
      this.reset();
      this.open = true;
      this.title = "添加设备档案信息";
    },
    /** 修改按钮操作 */
    handleUpdate(row) {
      this.reset();
      // console.log(this.$refs.checkdataRef);
      // this.$refs.checkdataRef.initfile();
      const id = row.id || this.ids
      getRecord(id).then(response => {
        this.form = response.data;
        // console.log("this.form",this.form)
        this.device=this.form.zyxDevice;
        this.dept=this.form.sysDept;
        // console.log("device:",this.device);
        this.open = true;
        this.title = "修改设备档案信息";
      });
    },
    /** 提交按钮 */
    submitForm() {
      this.$refs["form"].validate(valid => {
        if (valid) {
          if (this.form.id != null) {
            updateRecord(this.form).then(response => {
              this.msgSuccess("修改成功");
              this.open = false;
              this.getList();
            });
          } else {
            addRecord(this.form).then(response => {
              this.msgSuccess("新增成功");
              this.open = false;
              this.getList();
            });
          }
        }
      });
    },
    /** 删除按钮操作 */
    handleDelete(row) {
      const ids = row.id || this.ids;
      this.$confirm('是否确认删除设备档案信息?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return delRecord(ids);
        }).then(() => {
          this.getList();
          this.msgSuccess("删除成功");
        })
    },
    /** 导出按钮操作 */
    handleExport() {
      const queryParams = this.queryParams;
      this.$confirm('是否确认导出所有设备档案信息数据项?', "警告", {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning"
        }).then(function() {
          return exportRecord(queryParams);
        }).then(response => {
          this.download(response.msg);
        })
    }
  }
};
</script>
